package com.witdata.controller.community;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.ArticleDto;
import com.witdata.common.domain.vo.ArticleInfoVo;
import com.witdata.common.domain.vo.ArticleListWithLikeAndCommentCountVo;
import com.witdata.common.domain.vo.ArticleVo;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.service.community.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/community/article")
@RequiredArgsConstructor
public class ArticleController {
    private final ArticleService articleService;

    @GetMapping("/list")
    public Result<List<ArticleDto>> getArticleList(@RequestParam("communityId") Long communityId) {
        List<ArticleDto> articleList = articleService.getArticleList(communityId);
        return Result.ok(Status.SUCCESS, articleList);
    }

    @GetMapping("/list/tag")
    public Result<List<ArticleVo>> getArticleListByTagId(@RequestParam("communityId") Long communityId, @RequestParam("tagId") Long tagId) {
        List<ArticleVo> articleList = articleService.getArticleListByTagId(communityId, tagId);
        return Result.ok(Status.SUCCESS, articleList);
    }

    @PostMapping("/publish")
    public Result<String> publishArticle(@RequestBody ArticleDto articleDto) {
        if (ObjectUtils.isEmpty(articleDto) || ObjectUtils.isEmpty(articleDto.getAuthorId()) || ObjectUtils.isEmpty(articleDto.getCommunityId())
            || ObjectUtils.isEmpty(articleDto.getTagId())
        ) {
            throw new GlobalException(Status.PARAMERROR);
        }
        articleService.publishArticle(articleDto);
        return Result.ok(Status.PublishSuccess);
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/delete")
    public Result<String> deleteArticle(@RequestParam("articleId") Long articleId) {
        articleService.deleteArticle(articleId);
        return Result.ok(Status.DELETESUCCESS);
    }

    /**
     * 获取文章列表包含点赞数和评论数已经是否点赞
     */
    @GetMapping("/list/info")
    public Result<List<ArticleListWithLikeAndCommentCountVo>> getArticleListWithLikeAndCommentCount(@RequestParam("communityId") Long communityId) {
        List<ArticleListWithLikeAndCommentCountVo> articleList = articleService.getArticleListWithLikeAndCommentCount(communityId);
        return Result.ok(Status.SUCCESS, articleList);
    }

    /**
     * 根据文章id获取文章详情和评论
     */
    @GetMapping("/info")
    public Result<ArticleInfoVo> getArticleInfo(@RequestParam("articleId") Long articleId) {
        ArticleInfoVo articleInfo = articleService.getArticleInfo(articleId);
        return Result.ok(Status.SUCCESS, articleInfo);
    }
}
